//
//  PatManger.m
//  随拍
//
//  Created by lanou3g on 16/1/18.
//  Copyright © 2016年 Ashoka. All rights reserved.
//

#import "PatManger.h"
#import "PatForModel.h"

#define URL_Video @"https://newapi.meipai.com/channels/feed_timeline.json?id=3&type=2&feature=hot&page=1&language=zh-Hans&client_id=1089857302&device_id=866769024708664&version=4500&channel=bao360&model=NX511J&locale=1&sig=276cffa91b3b541eedd1133a2ed4c6ad&sigVersion=1.0"
@implementation PatManger

+(PatManger *)sharedPatManger{
    static PatManger *patManger = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        patManger = [[PatManger alloc] init];
        [patManger requestNetWork];
        
    });
    
    return patManger;
}


-(void)requestNetWork{
    
    NSURL *url = [NSURL URLWithString:URL_Video];
    
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
       
        NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
        for (NSDictionary *dict in array) {
            PatForModel *patModel = [[PatForModel alloc] init];
            
            [patModel setValuesForKeysWithDictionary:dict[@"media"]];
            
            [self.dataArray addObject:patModel];
      
           
        }
        
        for (NSDictionary *dic in array) {
            PatForModel *pat = [[PatForModel alloc] init];
            [pat setValuesForKeysWithDictionary:dic[@"media"][@"user"]];
            
            [self.arrays addObject:pat];
            
        }
       
  
        dispatch_async(dispatch_get_main_queue(), ^{
            
//
            self.updateUI();
        });
        
    }];
    
    
    
    
    
    
    [task resume];
    
    
    
    
}



-(PatForModel *)returnModelByIndex:(NSInteger)index{
    return [self.dataArray objectAtIndex:index];
}

-(NSMutableArray *)arrays{
    if (!_arrays) {
        self.arrays = [NSMutableArray array];
    }
    return _arrays;
}


-(NSMutableArray *)dataArray{
    if (! _dataArray) {
        self.dataArray = [NSMutableArray array];

    }
    
    
    return _dataArray;
}



@end
